package com.bst.bsj.bops.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.bst.bsj.base.controller.WebBaseController;
import com.bst.bsj.bops.web.biz.AreaService;
import com.bst.bsj.bops.web.biz.SeedlingWareHouseService;
import com.bst.bsj.common.dal.dataobject.Area;
import com.bst.bsj.common.dal.dataobject.SeedlingWareHouse;
import com.homewaimai.common.lang.Paginator;
import com.homewaimai.common.lang.Result;
import com.homewaimai.common.lang.StringUtil;

@Scope("prototype")
@Controller
@RequestMapping(value = "/bops/sys/sWareHouse")
public class SeedlingWareHouseController extends WebBaseController {

  @Autowired
  private SeedlingWareHouseService seedlingWareHouseService;
  @Autowired
  private AreaService areaService;

  /**
   * 
  * @Title: auditList
  * @Description: TODO(企业审核管理列表)
  * @param req
  * @param resp
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 上午9:37:34
  * @modificator :tonglu
  * @modify date :2016年5月13日 上午9:37:34
   */
  @SuppressWarnings("unchecked")
  @RequestMapping(value = "/sWareHouseList")
  public ModelAndView auditList(HttpServletRequest req, HttpServletResponse resp) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    Map<String, Object> paramsMap = new HashMap<String, Object>();
    ModelAndView modelAndView = getBopsModelAndView("company/nursery");
    String name = (String) params.get("name");//名称
    String provinceNumber = (String) params.get("provinceNumber");
    String cityNumber = (String) params.get("cityNumber");
    
    String scName = (String) params.get("scName");//企业名称
    String status = (String) params.get("status");//状态
    if (name != null) {
      name = name.trim();
    }
    if ("全部".equals(status)) {
      status = "";
    }

    if (scName != null) {
      scName = scName.trim();
    }
    String pageNums = (String) params.get("pageNum");
    Integer pageNum = 0;
    if (StringUtil.isNotBlank(status)) {
      paramsMap.put("status", status);
    }
    if (StringUtil.isNotBlank(name)) {
      paramsMap.put("name", name);
    }
    if (StringUtil.isNotBlank(provinceNumber)) {
      paramsMap.put("provinceNumber", provinceNumber);
    }
    if (StringUtil.isNotBlank(cityNumber)) {
      paramsMap.put("cityNumber", cityNumber);
    }
    
    if (StringUtil.isNotBlank(scName)) {
      paramsMap.put("scName", scName);
    }
    if (StringUtil.isNumeric(pageNums)) {
      pageNum = Integer.valueOf(pageNums);
    }
    if (pageNum == null || pageNum == 0) {
      pageNum = 1;
    }
    paramsMap.put("page", pageNum);
    paramsMap.put("length", new Long(10));
    paramsMap.put("offset", new Long((pageNum - 1) * 10));
    Result result = seedlingWareHouseService.query(paramsMap);
    // 如果请求成功
    if (result.isSuccess()) {
      // 得到分页器
      Paginator paginator = (Paginator) result.getModels().get("paginator");
      // 得到数据
      List<SeedlingWareHouse> seedlingWareHouses = (List<SeedlingWareHouse>) result.getModels().get(
          "seedlingWareHouses");
      modelAndView.addObject("seedlingWareHouses", seedlingWareHouses);
      modelAndView.addObject("paginator", paginator);
    }
    modelAndView.addObject("params", params);
    
    List<Area> provinceList = areaService.getAreas("province"); //所有省
    modelAndView.addObject("provinceList", provinceList);
    
    return modelAndView;
  }

  /**
   * 
  * @Title: getSeedlingWareHouse
  * @Description: TODO(根据id获取对象)
  * @param req
  * @param resp
  * @param id
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 下午3:14:01
  * @modificator :tonglu
  * @modify date :2016年5月13日 下午3:14:01
   */
  @RequestMapping(value = "/getSeedlingWareHouse")
  public ModelAndView getSeedlingWareHouse(HttpServletRequest req, HttpServletResponse resp, String id)
      throws Exception {
    ModelAndView modelAndView = new ModelAndView();
    SeedlingWareHouse seedlingWareHouse = seedlingWareHouseService.getSeedlingWareHouse(Long.valueOf(id));
    modelAndView.addObject("seedlingWareHouse", seedlingWareHouse);
    return modelAndView;
  }

  /**
   * 
  * @Title: getSeedlingCompany
  * @Description: TODO(根据id修改状态)
  * @param req
  * @param resp
  * @param id
  * @return
  * @throws Exception
  * ModelAndView
  * @throws 
  * @creator     :tonglu
  * @create date :2016年5月13日 下午3:14:01
  * @modificator :tonglu
  * @modify date :2016年5月13日 下午3:14:01
   */
  @RequestMapping(value = "/changeStatus")
  public void changeStatus(HttpServletRequest req, HttpServletResponse resp, String id) throws Exception {
    Map<String, Object> params = getBopsParams(req);//参数
    String status = (String) params.get("status");//状态
    SeedlingWareHouse seedlingWareHouse = new SeedlingWareHouse();
    seedlingWareHouse.setId(Long.valueOf(id));
    seedlingWareHouse.setStatus(status);
    seedlingWareHouseService.update(seedlingWareHouse);
  }
}
